#include <bits/stdc++.h>
// 2025/01/21
// tag:
// Author: Zhang Muen
using namespace std;

int parents[30001], pos[30001], len[30001];

class UF
{
public:
    UF()
    {
        for (int i = 1; i <= 30000; i++)
            parents[i] = i, len[i] = 1;
    }

    int find(int i)
    {
        if (parents[i] != i)
        {
            int root = find(parents[i]);
            pos[i] += pos[parents[i]];
            parents[i] = root;
        }
        return parents[i];
    }

    void un(int i, int j)
    {
        int ri = find(i), rj = find(j);
        if (ri == rj)
            return;
        parents[ri] = rj;
        pos[ri] += len[rj], len[rj] += len[ri], len[ri] = 0;
    }
};

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    UF uf;

    for (int i = 0; i < t; i++)
    {
        char c;
        int a, b;
        cin >> c >> a >> b;
        if (c == 'M')
            uf.un(a, b);
        else
        {
            if (uf.find(a) == uf.find(b))
                cout << abs(pos[a] - pos[b]) - 1 << endl;
            else
                cout << -1 << endl;
        }
    }
    return 0;
}